from lxml import etree

"""
1. 获取所有tr标签 
2. 获取第2个tr标签
3. 获取所有class等于even的标签
4. 获取所有a标签的href属性
5. 获取所有的职位信息(纯文本)
"""

parser = etree.HTMLParser(encoding='utf-8')
html = etree.parse('tencent.html',parser=parser)

#1. 获取所有tr标签
# //tr
# trs = html.xpath("//tr")
# for tr in trs:
#     print(etree.tostring(tr,encoding='utf-8').decode('utf-8'))

# 2. 获取第2个tr标签
# trs = html.xpath("//tr[2]")[0]
# print(etree.tostring(trs,encoding='utf-8').decode('utf-8'))
# for tr in trs:
#     print(etree.tostring(tr,encoding='utf-8').decode('utf-8'))

# 3. 获取所有class等于even的tr标签
# trs = html.xpath("//tr[@class='even']")
# for tr in trs:
#     print(etree.tostring(tr,encoding='utf-8').decode('utf-8'))

# 4. 获取所有a标签的href属性
# astest = html.xpath("//a/@href")
# for a in astest:
#     print(a)

# 5. 获取所有的职位信息(纯文本)
trs = html.xpath("//tr[position()>1]")
positions = []

for tr in trs:
    # 在某个标签下，在执行xpath函数，在获取这个标签下的子孙元素
    # 那么应该在//之前加一个点 . 代表是在当前元素下获取
    href = tr.xpath(".//a/@href")[0]
    fullurl = 'http://hr.tencent.com/' + href
    title = tr.xpath("./td[1]//text()")
    category = tr.xpath("./td[2]//text()")
    address = tr.xpath("./td[4]//text()")
    publictime = tr.xpath("./td[5]//text()")

    position = {
        'url':fullurl,
        'title':title,
        'category':category,
        'address':address,
        'publictime':publictime
    }
    positions.append(position)

print(positions)